package model;

import controler.Helper;

public class Stages {

    private Boolean alcoholicFermentationFinished = Boolean.FALSE;
    private Boolean malolacticFermentationFinished = Boolean.FALSE;
    private Boolean storageFinished = Boolean.FALSE;
    private Integer alcoholicFermentationState = Integer.valueOf(0);
    private Integer malolacticFermentationState = Integer.valueOf(0);
    private Integer storageState = Integer.valueOf(0);

    public Stages() {
        super();
    }

    public void doAlcoholicFermentation() {
        AlcoholicFermentation alcoholicFermentation = new AlcoholicFermentation();
        alcoholicFermentation.start();
    }

    public void resetAlcoholicFermentation() {
        alcoholicFermentationFinished = Boolean.FALSE;
        alcoholicFermentationState = Integer.valueOf(0);
    }

    public void resetMalolacticFermentation() {
        malolacticFermentationFinished = Boolean.FALSE;
        malolacticFermentationState = Integer.valueOf(0);
    }

    public void resetStorage() {
        storageFinished = Boolean.FALSE;
        storageState = Integer.valueOf(0);
    }

    public void doMalolacticFermentation() {
        MalolacticFermentation malolacticFermentation = new MalolacticFermentation();
        malolacticFermentation.start();
    }

    public void doStorage() {
        Storage storage = new Storage();
        storage.start();
    }

    public Boolean isAlcoholicFermentationFinished() {
        return alcoholicFermentationFinished;
    }

    public Boolean isMalolacticFermentationFinished() {
        return malolacticFermentationFinished;
    }

    public Boolean isStorageFinished() {
        return storageFinished;
    }

    public Integer getStorageState() {
        return storageState;
    }

    public Integer getAlcoholicFermentationState() {
        return alcoholicFermentationState;
    }

    public Integer getMalolacticFermentationState() {
        return malolacticFermentationState;
    }

    private class AlcoholicFermentation extends Thread {
        @Override
        public void run() {
            while (!alcoholicFermentationFinished) {
                Helper.sleep(Constants.ALCOHOLIC_FERMENTATION_TIME / 100);
                alcoholicFermentationState = Integer.valueOf(alcoholicFermentationState.intValue() + 1);
                if (alcoholicFermentationState == 100) {
                    alcoholicFermentationFinished = Boolean.TRUE;
                }
            }
        }
    }

    private class MalolacticFermentation extends Thread {
        @Override
        public void run() {
            while (!malolacticFermentationFinished) {
                Helper.sleep(Constants.MALOLACTIC_FERMENTATION_TIME / 100);
                malolacticFermentationState = Integer.valueOf(malolacticFermentationState.intValue() + 1);
                if (malolacticFermentationState == 100) {
                    malolacticFermentationFinished = Boolean.TRUE;
                }
            }
        }
    }

    private class Storage extends Thread {
        @Override
        public void run() {
            while (!storageFinished) {
                Helper.sleep(Constants.STORAGE_TIME / 100);
                storageState = Integer.valueOf(storageState.intValue() + 1);
                if (storageState == 100) {
                    storageFinished = Boolean.TRUE;
                }
            }
        }
    }

}
